import request, { CustomResponse } from "../api/http_class";
import {
  EmployeeI,
  JsApiTicketI,
  UserI,
  WeixinOauthI,
  SearchParamsI,
  UploadedFileI,
  TenantI,
  RoleI,
  ResourceI,
} from "../types/system";
import {
  LoginReqI,
  CellphoneRegisterReqI,
  VerifyCodeReqI,
  WeixinOauthReqI,
  RegisterReqI,
} from "@/types/api";

export const userLogin = (data: LoginReqI): Promise<CustomResponse<UserI>> => {
  return request<LoginReqI, UserI>({
    url: "/login/",
    method: "POST",
    data,
  });
};

export const updateProfile = (data: any): Promise<CustomResponse<UserI>> => {
  return request<any, UserI>({
    url: "/profile/",
    method: "PUT",
    data,
  });
};

export const userRegister = (
  data: RegisterReqI
): Promise<CustomResponse<UserI>> => {
  return request<RegisterReqI, UserI>({
    url: "/users/",
    method: "POST",
    data,
  });
};

export const resetUserPassword = (data: any): Promise<CustomResponse<any>> => {
  return request<any, any>({
    url: `/users/password/${data.id}`,
    method: "PUT",
    data,
  });
};

export const createTenant = (
  data: TenantI
): Promise<CustomResponse<TenantI>> => {
  return request<TenantI, TenantI>({
    url: "/tenants/",
    method: "POST",
    data,
  });
};

export const deleteTenant = (id: string): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/tenants/${id}/`,
    method: "DELETE",
  });
};

export const updateTenant = (
  data: TenantI
): Promise<CustomResponse<TenantI>> => {
  return request<TenantI, TenantI>({
    url: `/tenants/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getTenantDetail = (
  id: string
): Promise<CustomResponse<TenantI>> => {
  return request<string, TenantI>({
    url: `/tenants/${id}/`,
    method: "GET",
  });
};

export const searchTenantList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: TenantI[] }>> => {
  return request<any, { totalCount: number; data: TenantI[] }>({
    url: `/tenants/?${paramsString}`,
    method: "GET",
  });
};

export const importEmployeeList = (): Promise<CustomResponse<any>> => {
  return request<any, any>({
    url: `/employees/import/`,
    method: "POST",
  });
};

export const searchEmployeeList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: EmployeeI[] }>> => {
  return request<any, { totalCount: number; data: EmployeeI[] }>({
    url: `/employees/?${paramsString}`,
    method: "GET",
  });
};

export const createRole = (data: RoleI): Promise<CustomResponse<RoleI>> => {
  return request<RoleI, RoleI>({
    url: "/roles/",
    method: "POST",
    data,
  });
};

export const deleteRole = (id: string): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/roles/${id}/`,
    method: "DELETE",
  });
};

export const updateRole = (data: RoleI): Promise<CustomResponse<RoleI>> => {
  return request<RoleI, RoleI>({
    url: `/roles/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getRoleDetail = (id: string): Promise<CustomResponse<RoleI>> => {
  return request<string, RoleI>({
    url: `/roles/${id}/`,
    method: "GET",
  });
};

export const searchRoleList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: RoleI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: RoleI[] }>({
    url: `/roles/?${paramsString}`,
    method: "GET",
  });
};

export const searchResourceList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: ResourceI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: ResourceI[] }>({
    url: `/resources/?${paramsString}`,
    method: "GET",
  });
};

export const createUser = (data: UserI): Promise<CustomResponse<UserI>> => {
  return request<UserI, UserI>({
    url: "/users/",
    method: "POST",
    data,
  });
};

export const deleteUser = (id: string): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/users/${id}/`,
    method: "DELETE",
  });
};

export const updateUser = (data: UserI): Promise<CustomResponse<UserI>> => {
  return request<UserI, UserI>({
    url: `/users/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getUserDetail = (id: string): Promise<CustomResponse<UserI>> => {
  return request<string, UserI>({
    url: `/users/${id}/`,
    method: "GET",
  });
};

export const searchUserList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: UserI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: UserI[] }>({
    url: `/users/?${paramsString}`,
    method: "GET",
  });
};

export const exportUserList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: UserI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: UserI[] }>({
    url: `/users/?${paramsString}`,
    method: "GET",
  });
};

// 微信公众号授权跳转后端处理 末尾不加/会引发CORS报错，返回301
// 'ParsedQs' is missing
export const weixinPublicOauth2Userinfo = (
  data: any
): Promise<CustomResponse<WeixinOauthI>> => {
  return request<WeixinOauthReqI, WeixinOauthI>({
    url: "/weixin/oauth2/userinfo/",
    method: "GET",
    data,
  });
};

export const weixinPublicOauth2Base = (
  data: any
): Promise<CustomResponse<WeixinOauthI>> => {
  return request<WeixinOauthReqI, WeixinOauthI>({
    url: "/weixin/oauth2/base/",
    method: "GET",
    data,
  });
};

export const weixinPublicOauth2Token = (
  data: WeixinOauthI
): Promise<CustomResponse<UserI>> => {
  return request<WeixinOauthI, UserI>({
    url: "/weixin/oauth2/token/",
    method: "POST",
    data,
  });
};

export const CellphoneRegister = (
  data: CellphoneRegisterReqI
): Promise<CustomResponse<UserI>> => {
  return request<CellphoneRegisterReqI, UserI>({
    url: "/sms/register/",
    method: "POST",
    data,
  });
};

export const refreshToken = (): Promise<CustomResponse<any>> => {
  return request<any, string>({
    url: "/token-refresh/",
    method: "GET",
  });
};

export const getCaptcha = (): Promise<CustomResponse<any>> => {
  return request<any, WeixinOauthI>({
    url: "/captcha/",
    method: "POST",
  });
};

// 提交
export const getVerifyCode = (
  data: VerifyCodeReqI
): Promise<CustomResponse<string>> => {
  return request<VerifyCodeReqI, string>({
    url: "/sms/send-verify-code/",
    method: "POST",
    data,
  });
};

// 带图片验证码的发短信申请
export const getVerifyCodeCaptcha = (
  data: VerifyCodeReqI
): Promise<CustomResponse<WeixinOauthI>> => {
  return request<VerifyCodeReqI, WeixinOauthI>({
    url: "/sms/send-verify-code/captcha/",
    method: "POST",
    data,
  });
};

export const weixinRegister = (
  data: WeixinOauthI
): Promise<CustomResponse<UserI>> => {
  return request<WeixinOauthI, UserI>({
    url: "/weixin/oauth2/register/",
    method: "POST",
    data,
  });
};

export const getJsApiTicket = (): Promise<CustomResponse<any>> => {
  return request<any, JsApiTicketI>({
    url: "/weixin/js-api-ticket/",
    method: "GET",
  });
};

export const getSignature = (data: any): Promise<CustomResponse<any>> => {
  return request<any, string>({
    url: "/weixin/signature/",
    method: "GET",
    data,
  });
};

export const registerEmployee = (
  data: EmployeeI
): Promise<CustomResponse<UserI>> => {
  return request<EmployeeI, UserI>({
    url: "/employees/register/",
    method: "POST",
    data,
    // headers: {
    //   "Content-Type": "multipart/form-data",
    // },
  });
};

export const submitEmployeeVerify = (
  data: any
): Promise<CustomResponse<EmployeeI>> => {
  return request<any, EmployeeI>({
    url: "/employees/submit/verify/",
    method: "POST",
    data,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

export const createWeixinTempMaterial = (
  data: any
): Promise<CustomResponse<UploadedFileI>> => {
  return request<any, UploadedFileI>({
    url: "/weixin/create/temp-material/",
    method: "POST",
    data,
  });
};

export const getWeixinTempMaterial = (
  data: any
): Promise<CustomResponse<string>> => {
  return request<any, string>({
    url: "/weixin/get/temp-material/",
    method: "GET",
    data,
  });
};

export const getMockPictures = (
  data: any
): Promise<CustomResponse<string[]>> => {
  return request<any, string[]>({
    url: "/get/pictures/",
    method: "GET",
    data,
  });
};
